package com.hm.health.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import androidx.annotation.Nullable;

import com.goodix.comsdk.entity.MeasureResult;
import com.goodix.comsdk.sdk.OnHBDDataListener;
import com.goodix.iot.libbase.logger.Glog;
import com.goodix.libromablecom.sdk.DeviceDataType;
import com.hm.health.MyApplication;
import com.hm.health.Utils.CommonUtils;
import com.hm.health.notification.AppNotification;

import java.util.Arrays;
import java.util.Locale;

public class NotificationDataService extends Service {
    public static final String TAG = "NotificationDataService";
    private AppNotification mAppNotification;

    public NotificationDataService() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Glog.d(TAG, "onCreate: ");
        mAppNotification = new AppNotification(getApplication());
        MyApplication.getHBDTestClient().registerHBDDataListener(mOnHBDDataListener);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Glog.d(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Glog.d(TAG, "onDestroy: ");
        MyApplication.getHBDTestClient().unRegisterHBDDataListener(mOnHBDDataListener);
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    private OnHBDDataListener mOnHBDDataListener = new OnHBDDataListener() {
        @Override
        public void onDataChanged(String dataType, int[] dataArr) {
            Glog.d(TAG, "onDataChanged: " + dataType + ", " + Arrays.toString(dataArr));
            if (DeviceDataType.DATA_TYPE_BATTERY_INFO.equals(dataType)) {
                CommonUtils.runOnUIThread(() -> {
                    String current = String.format(Locale.ENGLISH,"%4d mA", dataArr[0]);
                    String voltage = String.format(Locale.ENGLISH,"%4d %%", dataArr[1]);
                    String txCurrent = String.format(Locale.ENGLISH,"%4d mA", dataArr[2]);
                    mAppNotification.buildNotficationCustom(current, voltage, txCurrent);
                });
            }
        }

        @Override
        public void onMeasureResult(MeasureResult result) {

        }
    };
}
